package com.xxw.yygh.hosp.controller.admin;



import com.xxw.yygh.common.result.R;
import com.xxw.yygh.common.utils.MD5;
import com.xxw.yygh.model.hosp.HospitalSet;
import com.xxw.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxw.yygh.hosp.service.HospitalSetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * 医院设置表(HospitalSet)表控制层
 *
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    //查询所有的医院设置信息
    @ApiOperation(value = "查询所有的医院设置信息")
    @GetMapping("/findAll")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("item",list);
    }

    //根据医院设置id删除医院设置信息
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id){
        hospitalSetService.removeById(id);
        return R.ok();
    }

    //分页查询医院设置信息
    @PostMapping("/page/{pageNum}/{size}")
    public R getPageInfo(@PathVariable Integer pageNum,
                         @PathVariable Integer size,
                         @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(pageNum, size);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())) {
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())) {
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        hospitalSetService.page(page,queryWrapper);
        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }


    //新增
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet){
        //设置状态 1 使用 2 不能使用
        hospitalSet.setStatus(1);
        //当前时间戳+随机数+MD5加密
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    //修改之回显数据
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id){
        return R.ok().data("item",hospitalSetService.getById(id));
    }

    //修改数据
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //批量删除
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Integer> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    //锁定与解锁
    @PutMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }


}

